﻿using DGIS.Service.IService.IDataService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;

namespace DGIS.Service.Service.DataService
{
    public class DataCreateService : IDataCreateService
    {
        public IFeatureClass CreateMemoryFeatureClass(ISpatialReference spatialReference, esriGeometryType geometryType, Dictionary<string, esriFieldType> fieldsDic)
        {
            string name = Guid.NewGuid().ToString();

            // 创建内存工作空间  
            IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();
            IWorkspaceName workspaceName = workspaceFactory.Create("", name, null, 0);
            IName pName = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)pName.Open();

            return CreateFeatureClasss(spatialReference, geometryType, fieldsDic, name, workspace);
        }

        public IFeatureClass CreateShpFeatureClass(ISpatialReference spatialReference, esriGeometryType geometryType, Dictionary<string, esriFieldType> fieldsDic, string shpPath)
        {
            string folder = System.IO.Path.GetDirectoryName(shpPath);
            string name = System.IO.Path.GetFileNameWithoutExtension(shpPath);

            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspaceName workspaceName = workspaceFactory.Create(folder,name,null, 0);

            IName pName = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)pName.Open();

            return CreateFeatureClasss(spatialReference, geometryType, fieldsDic, name, workspace);
        }

        private IFeatureClass CreateFeatureClasss(ISpatialReference spatialReference, esriGeometryType geometryType, Dictionary<string, esriFieldType> fieldsDic,string name,IWorkspace workspace)
        {            
            IFields fields = new FieldsClass();
            IFieldsEdit fieldsEdit = fields as IFieldsEdit;

            foreach (KeyValuePair<string, esriFieldType> item in fieldsDic)
            {
                IField field = new FieldClass();
                IFieldEdit fieldEdit = field as IFieldEdit;

                fieldEdit.Name_2 = item.Key;
                fieldEdit.Type_2 = item.Value;

                fieldsEdit.AddField(field);
            }

            //设置geometry字段
            IGeometryDef geometryDef = new GeometryDefClass();
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
            geometryDefEdit.AvgNumPoints_2 = 5;
            geometryDefEdit.GeometryType_2 = geometryType;
            geometryDefEdit.GridCount_2 = 1;
            geometryDefEdit.HasM_2 = false;
            geometryDefEdit.HasZ_2 = false;
            geometryDefEdit.SpatialReference_2 = spatialReference;

            IField geoField = new FieldClass();
            IFieldEdit geoFieldEdit = geoField as IFieldEdit;
            geoFieldEdit.Name_2 = "SHAPE";
            geoFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            geoFieldEdit.GeometryDef_2 = geometryDefEdit;
            geoFieldEdit.IsNullable_2 = true;
            geoFieldEdit.Required_2 = true;
            fieldsEdit.AddField(geoField);

            //创建featureclass
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
            IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(name, fields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");

            return featureClass;
        }
    }
}
